package com.example.car.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.car.CreateMessageSolution;
import com.example.car.entity.Car_login;
import com.example.car.mapper.CarMapper;
import com.example.car.receivemessages;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.huaweicloud.sdk.core.http.HttpRequest;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;


@Controller
@MapperScan("com.example.car.mapper")
public class LoginController {

    @Autowired
    private CarMapper carmapper;
    @RequestMapping("/login")
    public String loginindex(@RequestParam("username") String username, @RequestParam("password") String password, Model model){
       Car_login count = carmapper.selectByNameAndPassword(username, password);

       if (count == null){
           return "/login";

       }

        return "/index";

    }

    @RequestMapping("/index")
    public String index(){

        return "/index";
    }
    @RequestMapping("/")
    public String login(){

        return "/login";
    }

    @RequestMapping("/home")
    public String welcome(){

        return "/home";
    }
    @RequestMapping("/downMessage")
    @ResponseBody
        public String downMessage(String deviceId,String downMessage ) throws JsonProcessingException {
        CreateMessageSolution CreateMessageSolution = new CreateMessageSolution();
        String message = CreateMessageSolution.downMessage(deviceId,downMessage);
        return message;
    }
    @RequestMapping("/iot")
    public String iot(){
        return "/iot";
    }


    @RequestMapping(value = "/receiveMessages", method = RequestMethod.POST )
    @ResponseBody
    public String receiveMessages(@RequestBody String request){
        System.out.println(request);
        JSONObject Json = new JSONObject();
        String message = Json.toJSONString(request);
        System.out.println(message);
        String a = request.toString();
        return a;
    }
   /* @RequestMapping(value = "/receiveMessages", method = RequestMethod.GET )
    @ResponseBody
    public String receiveMessages(@RequestBody Map request){
        System.out.println(request.toString());
        String a = request.toString();
        return a;
    }*/

}



